package json.org.noahx.javavsjson;

import com.google.gson.Gson;

import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: noah
 * Date: 3/8/13
 * Time: 10:02 PM
 * To change this template use File | Settings | File Templates.
 */
public class GsonSerializationTest implements SerializationTest {

    private Gson gson;

    public GsonSerializationTest() {
        gson = new Gson();
    }

    @Override
    public String getTestName() {
        return "Gson";
    }

    @Override
    public Map<String, Object> testBytes2Map(byte[] bytes) {
        Map<String, Object> result = null;
        try {
            result = gson.fromJson(new String(bytes, "UTF-8"), Map.class);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public byte[] testMap2Bytes(Map<String, Object> map) {
        String str = gson.toJson(map);
        byte[] bytes = null;
        try {
            bytes = str.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return bytes;
    }
}
